package com.huatai.bpmn.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.bpmn.constant.PMPConstant;
import com.huatai.bpmn.entity.YYDataInfoEntity;
import com.huatai.bpmn.entity.vo.DataVo;
import com.huatai.bpmn.mapper.YYDataInfoMapper;
import com.huatai.bpmn.service.BasicLoadService;
import com.huatai.bpmn.service.YYDataInfoService;
import com.huatai.common.entity.UserInfo;
import com.huatai.common.util.ObjectUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.user.dubbo.UserDubboApi;
import com.huatai.user.entity.Department;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yxy
 */
@Service
public class YYDataInfoServiceImpl extends ServiceImpl<YYDataInfoMapper, YYDataInfoEntity>
	implements YYDataInfoService {

	@DubboReference(timeout = 30000, check = false, retries = 0)
	private UserDubboApi userDubboApi;
	@Autowired
	private YYDataInfoService yyDataInfoService;
	@Autowired
	private BasicLoadService basicLoadService;

	/**
	 * 根据用户，项目流程key获取流程id
	 *
	 * @param myKey
	 * @return
	 */
	@Override
	public JSONObject getDataList(String myKey, UserInfo user, String departName, int pgIndex, int pgSize, DataVo dataVo) {
		String grade = dataVo.getGrade();//等级
		String startTime = dataVo.getStartTime();//预计签约开始时间
		String endTime = dataVo.getEndTime();//预计签约结束时间
		String name = dataVo.getName();//预计签约结束时间
		pgIndex=(pgIndex-1)*pgSize;
		JSONObject json = new JSONObject();
		List<String> flowList = new ArrayList<>();
		int total=0;
		//预警项目需要提示
		if ("yujing".equals(myKey)){
			flowList=this.getBaseMapper().getYJFlowList(pgIndex,pgSize);
			total=this.getBaseMapper().getYJFlowListCount();
			json.put("total",total);
			json.put("flowList",flowList);
			return json;
		}
		String str = getStr(departName, grade, startTime, endTime, name);
		//如果用户角色为总经理-市场部-财务部则可以查看全部项目
		if (PMPConstant.USER_POST_ZJL.equals(user.getPostId().toString()) ||
			PMPConstant.DEPART_CWB_ID.equals(user.getDeptId().toString()) ||
			PMPConstant.DEPART_SCB_ID.equals(user.getDeptId().toString())||
		"admin".equals(user.getAccount())) {
			if (StringUtil.isEmpty(str)) {
				if (PMPConstant.TABLE_SS.equals(myKey)){
					flowList = this.getBaseMapper().getFlowListByMyKey(myKey,pgIndex,pgSize);
					total = this.getBaseMapper().getFlowListCountByMyKey(myKey);
				}else{
					flowList = this.getBaseMapper().getFlowList(myKey,pgIndex,pgSize);
					total = this.getBaseMapper().getFlowListCount(myKey);
				}
			} else {
				if (PMPConstant.TABLE_SS.equals(myKey)){
					flowList = this.getBaseMapper().getFlowListByDepartNameByMyKey(myKey, str,pgIndex,pgSize);
					total = this.getBaseMapper().getFlowListByDepartNameCountByMyKey(myKey, str);
				}else{
					flowList = this.getBaseMapper().getFlowListByDepartName(myKey, str,pgIndex,pgSize);
					total = this.getBaseMapper().getFlowListByDepartNameCount(myKey, str);
				}

			}
			json.put("total",total);
			json.put("flowList",flowList);
			return json;
		}
		Long deptId = user.getDeptId();
		Department department = userDubboApi.getDepart(deptId);
		String deptName = department.getDeptName();
		//获取部门信息，判断部门领导是不是当前用户
		if (ObjectUtil.isEmpty(department)) {
			throw new RuntimeException("请给用户赋予部门和角色");
		}
		//部门老大-主任-总工程师可以查看部门所有数据
		if (PMPConstant.USER_POST_ZR.equals(user.getPostId().toString())||PMPConstant.USER_POST_FZR.equals(user.getPostId().toString())||PMPConstant.USER_POST_ZGCS.equals(user.getPostId().toString())) {
			if (PMPConstant.TABLE_SS.equals(myKey)){
				if (StringUtil.isEmpty(str)) {
					flowList = basicLoadService.getFlowListByMyKey(myKey, deptName,pgIndex,pgSize);
					total = basicLoadService.getFlowListCountByMyKey(myKey, deptName);
				}else {
					flowList = basicLoadService.getFlowListByMyKeyStr(myKey, deptName,pgIndex,pgSize,str);
					total = basicLoadService.getFlowListCountByMyKeyStr(myKey, deptName,str);
				}
			}else{
				if (StringUtil.isEmpty(str)) {
					flowList = basicLoadService.getFlowList(myKey, deptName,pgIndex,pgSize);
					total = basicLoadService.getFlowListCount(myKey, deptName);
				}else {
					flowList = basicLoadService.getFlowListStr(myKey, deptName,pgIndex,pgSize,str);
					total = basicLoadService.getFlowListCountStr(myKey, deptName,str);
				}
			}

		} else {
			if (PMPConstant.TABLE_SS.equals(myKey)){
				if (StringUtil.isEmpty(departName)) {
					flowList = basicLoadService.getFlowList1ByMyKey(myKey, user.getRealName(),pgIndex,pgSize);
					total = basicLoadService.getFlowList1CountByMyKey(myKey, user.getRealName());
				}else {
					flowList=null;
				}
			}else{
				if (StringUtil.isEmpty(str)) {
					List<String> flowList1 = basicLoadService.getFlowList1(myKey,PMPConstant.KEY_XMJL, user.getRealName(), pgIndex, pgSize);//当前用户是项目经理的项目
					List<String> flowList2 = basicLoadService.getFlowList1(myKey, PMPConstant.Key_JOIN_PEOPLE,user.getRealName(), pgIndex, pgSize);//实施参与人员
					if (flowList1.size()>0){
					flowList.addAll(flowList1);
					}
					if (flowList2.size()>0){
						flowList.addAll(flowList2);
					}
					int num1 = basicLoadService.getFlowList1Count(myKey,PMPConstant.KEY_XMJL, user.getRealName());
					int num2 = basicLoadService.getFlowList1Count(myKey,PMPConstant.Key_JOIN_PEOPLE, user.getRealName());
					total+=num1;
					total+=num2;
				}else {
					flowList = basicLoadService.getFlowList1ByName(myKey, user.getRealName(),pgIndex,pgSize,str);
					total = basicLoadService.getFlowList1CountByName(myKey, user.getRealName(),str);
				}
			}

		}
		json.put("total",total);
		json.put("flowList",flowList);
		return json;
	}

	private String getStr(String departName, String grade, String startTime, String endTime, String name) {
		String str="";
		if (!StringUtil.isEmpty(departName)){
			str+="and b.gkbm=\""+departName+"\"";
		}
		if (!StringUtil.isEmpty(grade)){
			str+="and b.xmdj=\""+grade+"\"";
		}
		if (!StringUtil.isEmpty(name)){
			str+="and b.xmmc=\""+name+"\"";
		}
		if (!StringUtil.isEmpty(startTime)&&!StringUtil.isEmpty(endTime)){
			str+="and b.yjqdsj BETWEEN \""+startTime+"\" and"+"\""+endTime+"\"";
		}
		return str;
	}

	/**
	 * 根据flowId删除数据
	 *
	 * @param flowId
	 */
	@Override
	public void removeByFlowId(String flowId) {
		this.remove(Wrappers.<YYDataInfoEntity>lambdaQuery().eq(YYDataInfoEntity::getFlowId, flowId));
	}

	/**
	 * 数据转入实施库
	 *
	 * @param businessKey
	 * @return
	 */
	@Override
	public boolean shiftSS(String businessKey, String myKey) {
		YYDataInfoEntity yyDataInfoEntity = this.getOne(Wrappers.<YYDataInfoEntity>lambdaQuery().eq(YYDataInfoEntity::getFlowId, businessKey));

		if (ObjectUtil.isEmpty(yyDataInfoEntity)) {
			throw new RuntimeException("没有该项目数据");
		}
		//如果为实施的话需要记录数据来源
		yyDataInfoEntity.setMyKey(myKey);
		/*if (PMPConstant.TABLE_SS.equals(myKey)){
			yyDataInfoEntity.setSource(myKey);
		}else{
			yyDataInfoEntity.setMyKey(myKey);
		}
		if (PMPConstant.TABLE_HMD.equals(myKey)){
			yyDataInfoEntity.setSource("");
		}*/

		return this.updateById(yyDataInfoEntity);
	}



	@Override
	public List<String> yearListByDepart(String deptName,String str) {
		return this.getBaseMapper().yearListByDepart(deptName,str);
	}

	@Override
	public YYDataInfoEntity getBusinessKey(String businessKey) {
		return this.getOne(Wrappers.<YYDataInfoEntity>lambdaQuery().eq(YYDataInfoEntity::getFlowId,businessKey));
	}

	@Override
	public List<String> tdYearList(int year) {
		return this.getBaseMapper().tdYearList(year);
	}

	@Override
	public List<String> listByYear(int year,String myKey) {
		return this.getBaseMapper().listByYear(year,myKey);
	}

	@Override
	public List<String> ListByDepart(String deptName,int year) {
		return this.getBaseMapper().ListByDepart(deptName,year);
	}

	@Override
	public List<String> myKeyList(String myKey) {
		return this.getBaseMapper().myKeyList(myKey);
	}

}




